package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.LogType;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysLogMapper;
import com.mmall.dao.SysRoleUserMapper;
import com.mmall.dao.SysUserMapper;
import com.mmall.pojo.SysLogWithBLOBs;
import com.mmall.pojo.SysRoleUser;
import com.mmall.pojo.SysUser;
import com.mmall.service.ISysRoleUserService;
import com.mmall.util.IpUtil;
import com.mmall.util.JsonUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author wangtengyu
 * @Create 2018-03-16-16:08
 */
@Service("iSysRoleUserService")
public class SysRoleUserServiceImpl implements ISysRoleUserService{

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysLogMapper sysLogMapper;

    /**
     * 根据角色id获得用户列表
     * @param roleId
     * @return
     */
    public List<SysUser> getUserListByRoleId(int roleId){
        List<Integer> userIdList=sysRoleUserMapper.getUserIdListByRoleId(roleId);
        if(CollectionUtils.isEmpty(userIdList)){
            return Lists.newArrayList();
        }
        return sysUserMapper.getByIdList(userIdList);
    }


    public void changeUsers(int roleId, List<Integer> userIdList){
        List<Integer> oldUserIdList=sysRoleUserMapper.getUserIdListByRoleId(roleId);
        if(oldUserIdList.size()==userIdList.size()){
            Set<Integer> oldUserIdSet= Sets.newHashSet(oldUserIdList);
            Set<Integer> userIdSet= Sets.newHashSet(userIdList);
            oldUserIdSet.remove(userIdSet);
            if(CollectionUtils.isEmpty(oldUserIdSet)){
                return;
            }
        }
        updateRoleUsers(roleId,userIdList);
        saveRoleUserLog(roleId,oldUserIdList,userIdList);
    }

    @Transactional
    public  void updateRoleUsers(int roleId, List<Integer> userIdList) {
        if(CollectionUtils.isEmpty(userIdList)){
            sysRoleUserMapper.deleteByRoleId(roleId);
        }

        List<SysRoleUser> roleUserList=Lists.newArrayList();

        for(Integer userId:userIdList){
            SysRoleUser roleUser=SysRoleUser.builder().roleId(roleId).userId(userId).operator(RequestHolder.getCurrentUser().getUsername())
                    .operateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest())).operateTime(new Date()).build();
            roleUserList.add(roleUser);
        }
        sysRoleUserMapper.deleteByRoleId(roleId);
        sysRoleUserMapper.batchInsert(roleUserList);
    }

    private void saveRoleUserLog(int roleId, List<Integer> before, List<Integer> after) {
        SysLogWithBLOBs sysLog = new SysLogWithBLOBs();
        sysLog.setType(LogType.TYPE_ROLE_USER);
        sysLog.setTargetId(roleId);
        sysLog.setOldValue(before == null ? "" : JsonUtil.obj2String(before));
        sysLog.setNewValue(after == null ? "" : JsonUtil.obj2String(after));
        sysLog.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysLog.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysLog.setOperateTime(new Date());
        sysLog.setStatus(1);
        sysLogMapper.insertSelective(sysLog);
    }
}
